#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "include/curl.h"

#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_ftpManager = new FtpManager(this);
    connect(m_ftpManager, &FtpManager::uploadFinished, this, &MainWindow::onUploadFinished);
}

MainWindow::~MainWindow()
{
    delete ui;
}
// ftp网址 ftp://sftpuser@192.168.0.205/uploads/
// user ->sftpuser
// pwd ->
// ftpServer->192.168.0.205
void MainWindow::on_pushButton_clicked()
{
    QString ftpServer = "192.168.0.205";
    QString user = "sftpuser";
    QString pwd = "th123456";

    QString localPath = QFileDialog::getOpenFileName(
        this, "选择要上传的文件", "",
        "所有文件 (*);;压缩文件 (*.zip *.rar);;图片文件 (*.jpg *.png)"
        );

    if (!localPath.isEmpty()) {
        QFileInfo fileInfo(localPath);
        QString remotePath = "uploads/" + fileInfo.fileName();

        // 重置进度条
        ui->progressBar->setRange(0, 100);
        ui->progressBar->setValue(0);
        ui->progressBar->show();

        // 开始上传
        m_ftpManager->uploadFile(ftpServer, user, pwd, localPath, remotePath, ui->progressBar);
    }

}

void MainWindow::onUploadFinished(bool success, const QString &errorMsg)
{
    if (success) {
        QMessageBox::information(this, "成功", "文件上传完成");
    } else {
        QMessageBox::warning(this, "失败", errorMsg);
    }
}

